<?php
$txt=file_get_contents('php://input');
logfile($txt,'nuomi_return_log.txt');

function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

/*$txt='{"VerifyCode":"34fa98f1d8822da8bab8813e955492c0","Order16U":"216755","ActionTime":"2014-07-18 14:42:57","OrderCall":"341990511_212863685","Tnumber":"2","OrderState":1}
';*/
$date = json_decode($txt);

$custId = '123545'; //二维码商帐号
$p_encode = '123545'; //二维码商密钥
$time=  strtotime($date->ActionTime); //获取时间戳
$encode = md5($p_encode.$time.$custId); //加密签名
$third =  explode('_',$date->OrderCall); //糯米的订单号截取
$thirdOrderId = $third[0]; //用户订单号
$thirdTicketId = $third[1]; //用户券标识

//认证用户账号跟token
//正式的账号跟地址
$auInfo = array('userName'=>'PFT_nm','token'=>'lblVElIUFRhTcbZYv3VdI5ra');
$url = 'http://y.nuomi.com/tapi/tuan/out/cert/qrcode/payment?auth={"userName":'.$auInfo['userName'].',"token":'.$auInfo['token'].'}';//百度糯米提供回调的地址
//测试的账号跟地址
/*$auInfo = array('userName'=>'lbc_test','token'=>'9a0e839de69962df88a08b256002ab60');//测试
$url = 'http://61.135.186.173:8085/tapi/tuan/out/cert/qrcode/payment?auth={"userName":'.$auInfo['userName'].',"token":'.$auInfo['token'].'}';
*/
$nm_array = array('custId'=>$custId,'encode'=>$encode,'time'=>$time,'orderId'=>$date->Order16U,'thirdOrderId'=>$thirdOrderId,'thirdTicketId'=>$thirdTicketId);

$key = array_keys($nm_array);
$value = array_values($nm_array);

// echo $result = make_post($key,$value,$url);
$result = make_post($key,$value,$url);
$result = json_decode($result,true);
if($result['msg'] == 'success'){
	echo 200;
}
  
function make_post($key,$value,$url){
	$post_data = array();
	if(!is_array($key) && !is_array($value)){
		$post_data[$key] = $value; 
	}
	else{
		foreach ($key as $kk=>$vv){
			$post_data[$vv]=$value[$kk];	
		}
	}
	$o=""; 
	foreach ($post_data as $k=>$v){ 
	$o.= "$k=".urlencode($v)."&"; 
	}
	$post_data=substr($o,0,-1); 
	err('----------------------回调提交参数---------------------','nm_debug.txt');
	err($post_data,'nm_debug.txt');

	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_POST, 1); 
	curl_setopt($ch, CURLOPT_HEADER, 0); 
	curl_setopt($ch, CURLOPT_URL,$url); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	return curl_exec($ch);
}  
function err($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

?>